package com.shandao.shangxiu.net;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.widget.ImageView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.shandao.shangxiu.AppContext;
import com.shandao.shangxiu.R;
import com.shandao.shangxiu.bean.response.ResponseBase;
import com.shandao.shangxiu.interf.VolleyInterface;
import com.shandao.shangxiu.util.Constants;
import com.shandao.shangxiu.util.Rsa;
import com.shandao.shangxiu.util.Utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


/**
 * HTTP所有请求方法管理类
 * Created by Administrator on 2015/7/8.
 */
public class API {

    //根据TAG取消网络请求
    public static void loadCancel(String tag) {
        HttpUtils.cancelAll(tag);
    }

    //获取图片，并指定默认图片的资源ID
    public static void getImage(Context context, String url, int defaultRes, ImageView
            imageView) {
        if (url == null) {
            imageView.setImageResource(getDefaultImage(defaultRes));
            return;
        }
        HttpUtils.getInstance(context).getImageLoader().get(url, ImageLoader.getImageListener
                        (imageView,getDefaultImage(defaultRes), getDefaultImage
                                (defaultRes)),
                imageView.getWidth(), imageView.getHeight());
    }

    /**
     * 获取不同尺寸的默认图片
     *
     * @param resId 资源ID
     * @return
     */
    public static int getDefaultImage(int resId) {
        if (resId == -1) {
            return R.mipmap.icon_image_default;
        } else {
            return resId;
        }
    }

    //获取图片，并使用特定的默认图片
    public static void getImage(Context context, String url, ImageView imageView) {
        getImage(context, url, -1, imageView);
    }

    public static void downloadImage(Context context, String url, final String filePath, final
    ImageDownload imageDownload) {
        if (TextUtils.isEmpty(url) || TextUtils.isEmpty(filePath)) {
            return;
        }
        HttpUtils.getInstance(context).getImageLoader().get(url, new ImageLoader.ImageListener() {
            @Override
            public void onResponse(ImageLoader.ImageContainer imageContainer, boolean b) {
                if (imageContainer.getBitmap() != null) {
                    File file = new File(filePath);
                    if (file.exists()) {
                        file.delete();
                    }
                    try {
                        FileOutputStream out = new FileOutputStream(file);
                        imageContainer.getBitmap().compress(Bitmap.CompressFormat.PNG, 90, out);
                        out.flush();
                        out.close();
                        imageDownload.onSuccess();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
    }

    public interface ImageDownload {
        void onSuccess();
    }
    
    


    public static HashMap<String, String> getBaseParams() {
        HashMap<String, String> params = new HashMap<>();
        params.put("serviceVerison", "1.0");
        params.put("deviceType", "android");
        params.put("appVerison", "1.0");
        params.put("appType", "1");
        params.put("ip", "192.168.1.2");
        return params;
    }

           public static void register(String tag, String phone, String password,
                String passwordConfirm, String nickname, String checkCode,
                String imeiCode, File file, VolleyInterface<ResponseBase> vif) {
            String url = Constants.BASE_URL;
            HashMap<String, String> params = getBaseParams();
            params.put("service", "register");
            params.put("mobile", phone);
            params.put("password", Rsa.encryptByPublic(password));
            params.put("passwordConfirm", Rsa.encryptByPublic(passwordConfirm));
            params.put("nickname", nickname);
            params.put("validateCode", checkCode);
            params.put("imeiCode", imeiCode);
            if (file == null) {
                GsonRequest<ResponseBase> request = new GsonRequest<>(
                        Request.Method.POST, url, ResponseBase.class, null, params,
                        vif.successListener(), vif.errorListener());
                HttpUtils.getInstance(AppContext.getInstance()).request(tag,
                        request);
            } else {
                RequestQueue requestQueue = HttpUtils.getSingleQueue();
                Map<String, File> files = new HashMap<>();
                files.put("avatar", file);
                MultiPartStringRequest<ResponseBase> multiPartRequest = new MultiPartStringRequest<ResponseBase>(
                        Request.Method.POST, url, ResponseBase.class, files,
                        params, vif.successListener(), vif.errorListener());
                requestQueue.add(multiPartRequest);
            }
	    }

}
